package br.odb.libscene;

import br.odb.utils.Utils;
import br.odb.utils.math.Vec3;

/**
 * 
 * @author Daniel "Monty" Monteiro
 */
public class Actor extends SceneObject {

	private int kind;
	private Vec3 previousPosition;	
	public int currentSector;
	public int candelas;
	private float angle;
	private int id;
	private Actor generated;
	private Actor owner;
	private boolean alive = true;
	
	public Actor getOwner() {
		return owner;
	}

	public void setAlive(boolean b) {
		alive = b;		
	}
	
	public boolean isAlive() {
		return alive;
	}


	
	public String toString() {
		return "a " + getCurrentSector() + " " + getEmissiveLightningIntensity() + " " + kind;
	}
	
	public int getCurrentSector() {
		return currentSector;
	}


	public Actor() {
		super();
		previousPosition = new Vec3();
		generated = null;
	}

	public Actor( Actor actor) {
		super( actor );
		
		owner = actor;
		generated = null;
		kind = actor.kind;
		previousPosition = new Vec3();
		currentSector = actor.currentSector;
		candelas = actor.candelas;
		angle = actor.angle;
		id = actor.id;
	}

	public void tick() {
		
	}
	
public void consume( int cmdCode ) {
		
		switch ( cmdCode ) {
		case ActorConstants.MOVE_N: {
				br.odb.utils.math.Vec2 translation = Utils.getTranslationForOctant( (int) (angle / 22.5f) );
				move( translation.x, 0.0f, -translation.y );
			}			
			break;
		case ActorConstants.MOVE_S:	{
				br.odb.utils.math.Vec2 translation = Utils.getTranslationForOctant( (int) (angle / 22.5f) + 8 );
				move( translation.x, 0.0f, -translation.y );
			}
			break;
		case ActorConstants.MOVE_W: {
				br.odb.utils.math.Vec2 translation = Utils.getTranslationForOctant( (int) (angle / 22.5f) + 12 );
				move( translation.x, 0.0f, translation.y );
			}
			break;
		case ActorConstants.MOVE_E: {
				br.odb.utils.math.Vec2 translation = Utils.getTranslationForOctant( (int) (angle / 22.5f) + 4 );
				move( translation.x, 0.0f, translation.y );
			}
			break;
		case ActorConstants.MOVE_DOWN:
			move( 0, -1, 0 );
			break;
		case ActorConstants.MOVE_UP:
			move( 0, 1, 0 );
			break;
		case ActorConstants.TURN_L:
			angle -= 22.5f;
			break;
		case ActorConstants.TURN_R:
			
			angle += 22.5f;
			
			
			break;
		case ActorConstants.FIRE:
			fire();
			break;
	
		}
	}

	public void setGenerated( Actor generated ) {
		this.generated = generated;		
	}


	public void fire() {
		setGenerated( generateCopy() );
	}

	public Actor generateCopy() {
		return new Actor( this );
	}

	public void scale(Vec3 scale) {
		
	}

	public float getAngleXZ() {	
		return angle;
	}
	
	public void setAngleXZ( float angle ) {
		this.angle = angle;
	}
	
	public void rotateXZ( float angle ) {
		this.angle += angle;
	}

	public void move( float x, float y, float z ) {
		Vec3 p = getPosition();
		previousPosition.copy( getPosition() );
		p.addTo( x, y, z );
	}
	
	public void move(Vec3 translate) {
		previousPosition.copy( getPosition() );		
		setPosition( getPosition().add( translate ) );		
	}

	/**
	 * 
	 * @param id
	 */
	public void setId( int id ) {
		this.id = id;
	}
	
	/**
	 * 
	 * @return
	 */
	public int getId() {
		return id;
	}
	public void undo() {		
		getPosition().copy( previousPosition );
	}

	public void setCurrentSector( int sector ) {
		currentSector = sector;
		
	}

	public void setEmissiveLightningIntensity(short light ) {
		candelas = light;
		
	}
	
	public Actor checkGenerated() {
		Actor toBeReturned = generated;
		generated = null;
		return toBeReturned;
	}
}
